package com.yuou.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuou.annotation.AccessLimit;
import com.yuou.annotation.SystemLog;
import com.yuou.controller.BaseExceptionController;
import com.yuou.model.Message;
import com.yuou.service.MessageService;
import com.yuou.util.ConstantUtil;
import com.yuou.util.UserIpUtil;

@Controller
@RequestMapping(value = "/admin")
public class MessageControllerAdmin  extends BaseExceptionController {
	@Resource(name = "messageServiceImpl")
	private MessageService messageService;
	
	
	/**
	  * 发送消息功能
	  * @param blog
	  * @return
	  * @throws Exception
	  */
	 @RequestMapping(value = "/addMessage",method = RequestMethod.POST)
	 @ResponseBody
	 @SystemLog(description = ConstantUtil.BLOGER_UPDATE,userType=ConstantUtil.USERTYPE_ADMIN) 
	 public Map<String, Object> addRole(String prarm,Message message,ServletRequest srequest) throws Exception{
		 HttpServletRequest request = (HttpServletRequest) srequest;
		 String ip=UserIpUtil.getIp(request);
		 message.setIp(ip);
		 Map<String, Object> map=new HashMap<String, Object>();
		 if(messageService.insertMessage(message)!=0){
			 map.put("status", 200);
		 }else{
			 //0表示：更新失败
			 map.put("status", 0);
		 }
		 return map;
	 }
	 
	 /**
	  * 更新信息状态功能
	  * @param blog
	  * @return
	  * @throws Exception
	  */
	 @RequiresPermissions("sys:message:edit")
	 @RequestMapping(value = "/updateMessage",method = RequestMethod.POST)
	 @ResponseBody
	 @SystemLog(description = ConstantUtil.BLOGER_UPDATE,userType=ConstantUtil.USERTYPE_ADMIN) 
	 public Map<String, Object> updateRole(String prarm,Message message) throws Exception{
		 Map<String, Object> map=new HashMap<String, Object>();
		 try {
		 if(messageService.updateByPrimaryKey(message)!=0){
			 map.put("status", 200);
		 }else{
			 //0表示：更新失败
			 map.put("status", 0);
		 }
		 }catch(Exception e) {
			 e.printStackTrace();
		 }
		 return map;
	 }
	 
	 /**
	  * 实现消息功能
	  * @param blog
	  * @return
	  * @throws Exception
	  */
	 @RequiresPermissions("sys:message:remove")
	 @RequestMapping(value = "/deleteMessage",method = RequestMethod.POST)
	 @ResponseBody
	 @SystemLog(description = ConstantUtil.BLOG_DELETE,userType=ConstantUtil.USERTYPE_ADMIN) 
	 public Map<String, Object> deleteBlog(Integer id) throws Exception{
		 Map<String, Object> map=new HashMap<String, Object>();
		 if(messageService.deleteByPrimaryKey(id)!=0){
			 map.put("status", 200);
		 }else{
			 //0表示：删除失败
			 map.put("status", 0);
		 }
		 return map;
	 }
	 
	 /**
		 * 模糊组合分页查询角色信息(and)
		 * @param bloger
		 * @param page
		 * @param pageSize
		 * @return
		 * @throws Exception
		 */
		 //@RequiresPermissions("sys:message:message")
		 @RequestMapping(value = "/selectAllMessage")
		 @ResponseBody
		 @AccessLimit(seconds=1,maxCount=15)
		 public Map<String, Object> selectAllblogerWithRoles(Message message,@RequestParam(value="page", required=true,defaultValue="1") Integer page,@RequestParam(value="pageSize", required=true,defaultValue="10") Integer pageSize) throws Exception{
			 Map<String, Object> map=new HashMap<String, Object>();
			 if(message.getIp()!=null&&message.getIp()!=""){
				 map.put("getIp", message.getIp());
			 }
			 if(message.getContent()!=null&&message.getContent()!=""){
				 map.put("content", message.getContent());
			 }
			 if(message.getStatus()!=null&&message.getStatus()!=0){
				 map.put("status", message.getStatus());
			 }
				 
			 //分页显示：第1页开始，每页显示10条记录
			 PageHelper.startPage(page, pageSize);
			 List<Message> messageList=messageService.selectAll(map);
			 PageInfo<Message> pageInfo=new PageInfo<Message>(messageList);
			 Map<String, Object> returnMap=new HashMap<String, Object>();
			 if(messageList.size()>0){
				 returnMap.put("status", 200);
			 }else{
				 //500表示：返回值为Null
				 System.out.println("MessageController查询结果为空============");
				 returnMap.put("status", 500);
			 }
			 
			 returnMap.put("messageList", messageList);
			 returnMap.put("pageInfo", pageInfo);
			 return returnMap;
		 }
}
